// (1) 通过这个类 watcher 实现更新

import pushTarget from "./dep"
import popTarget from "./dep"
let id = 0

export default class watcher {
    constructor(vm, updataComponent, cb, options) {
        // (1)
        this.vm = vm
        this.exprOrfn = updataComponent
        this.cb = cb
        this.options = options
        this.id = id++
        // 判断
        if(typeof updataComponent === 'function') {
            this.getter = updataComponent // 用来更新
        }
        // 更新视图
        this.get()
    }
    // 初次渲染
    get() {
        pushTarget(this) // 给dep 添加 watcher
        this.getter()
        popTarget() // 给 dep 取消 watcher
    }
    // 更新
    updata() {
        this.getter()
    }
}
